<?php
/**
 * 企业表 控制器
 * @author ThankPHP
 * @link Http://www.ThankPHP.Com
 * @copyright Power By Thankphp
 */
class EnterpriseAction extends Action {
	/**
	 * 查询所有的企业,并列表显示
	 */
	public function AdminIndex(){
		EasyACL('Enterprise','AdminIndex',$_SESSION);
		//实例化
		$Enterprise = D('enterprise');
		//计算数据库中所有的企业总数
		$count = $Enterprise->count('','enter_id');
		//每页显示的数量
		$listRows = 15;
		//需要显示的字段
		$fields = 'enter_id,euser_id,name,re_id,lock';
		//引入分页
		import('ORG.Util.Page');
		$p = new Page($count,$listRows);
		$list = $Enterprise->xfindall(null,$fields,'enter_id desc',$p->firstRow.','.$p->listRows);
		$page = $p->show();

		//设置到模板
		$this->assign('Enterprises',$list);
		$this->assign('Page',$page);
		$this->display();
	}

	/**
	 * 创建新的企业
	 */
	public function AdminCreate(){
		EasyACL('Enterprise','AdminCreate',$_SESSION);
		//POST提交
		if ($this->isPost()){
			if (!empty($_FILES['file']['name'])) {
				//上传文件
		        import("ORG.Net.UploadFile");
		        $upload = new UploadFile();
		        //设置上传文件大小
		        $upload->maxSize  = 329220 ;
		        //设置上传文件类型
		        $upload->allowExts  = explode(',','jpg,gif,png');
		        //设置附件上传目录
		        $upload->savePath =  './upload/'; 
		
		        //执行上传操作
		        if(!$upload->upload()) {
		            $this->assign("errMsg",$upload->getError());   
		        }else{ 
	        		$AllFiles = $upload->getSuccessFiles();
	        	}
			}
			//实例化输入类
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			
			$Enterprise = D('enterprise'); //实例化数据库操作
			$Enterprise->euser_id = $Input->post('euser_id'); //捆绑管理用户
			$Enterprise->domain = $Input->post('domain');//捆绑的域名
			$Enterprise->cid = $Input->post('cid'); //分类编号
			$Enterprise->name = htmlspecialchars($Input->post('name')); //名称
			if (isset($AllFiles)){
				$Enterprise->logo = './upload/'.$AllFiles[0]['savename'];
			}
			$Enterprise->enote = htmlspecialchars($Input->post('enote'));//简短介绍
			$Enterprise->tel = htmlspecialchars($Input->post('tel'));//电话
			$Enterprise->fax = htmlspecialchars($Input->post('fax'));//传真
			$Enterprise->contactpeople = htmlspecialchars($Input->post('contactpeople'));//联系人
			$Enterprise->address = htmlspecialchars($Input->post('address'));//地址
			$Enterprise->videocode = $Input->post('videocode');//视频播放代码
			$Enterprise->re_id = $Input->post('re_id');//推荐类型代码
			$Enterprise->videolock = $Input->post('videolock');//视频锁
			$Enterprise->created = time();
			$Enterprise->updated = time();
			$result = $Enterprise->add();
			if (!$result) {
				halt('尝试创建新的企业失败!');
			}
 
			//成功则直接跳转到企业列表
			//$this->redirect('AdminIndex','Enterprise');
		}
		//获取企业分类
		$Ecategory = D('ecategory');
		$Ecate = $Ecategory->findall();
		//获取推荐企业类型
		$Recommend = D('recommend');
		$recomm = $Recommend->findall();
		
		//输出到模板
		$this->assign('Ecate',$Ecate); //企业分类
		$this->assign('Recomm',$recomm);//所有推荐类型
		$this->display();
	}

	/**
	 * 设置锁定或解除锁定
	 */
	public function AdminUpdateLock(){
		EasyACL('Enterprise','AdminUpdateLock',$_SESSION);
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		//需要修改状态的企业
		$Enter_id = $Input->get('enter_id');
		//get当前状态
		$State = $Input->get('lock');
		
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		//查询enter_id 等于 $Enter_id 的企业
		$conditions->put('enter_id',array('EQ',$Enter_id));
		
		$Enterprise = D('enterprise');
		$Enterprise->where($conditions)->find();//查询要修改的信息
		//如果变量State等于'Y'，则设置数据库到N,否则设置成Y
		$Enterprise->lock = ($State == 'Y') ? 'N' : 'Y';
		$result = $Enterprise->save();
		if (!$result){
			halt('尝试设置状态失败!');
		}
		$this->redirect('AdminIndex','Enterprise');
	}
	
	/**
	 * 修改企业信息
	 */
	public function AdminUpdate(){
		EasyACL('Enterprise','AdminUpdate',$_SESSION);

		$Input = Input::getInstance();
		$Input->filter('addslashes');

		import('ORG.Util.HashMap');
		//Post提交
		if ($this->isPost()){
			if (!empty($_FILES['file']['name'])) {
				if (isset($_POST['logo'])){
					unlink($_POST['logo']);
				}
				//上传文件
		        import("ORG.Net.UploadFile");
		        $upload = new UploadFile();
		        //设置上传文件大小
		        $upload->maxSize  = 329220 ;
		        //设置上传文件类型
		        $upload->allowExts  = explode(',','jpg,gif,png');
		        //设置附件上传目录
		        $upload->savePath =  './upload/'; 
		
		        //执行上传操作
		        if(!$upload->upload()) {
		            $this->assign("errMsg",$upload->getError());   
		        }else{ 
	        		$AllFiles = $upload->getSuccessFiles();
	        	}
			}
			//设置查询条件
			$conditions = new HashMap();
			$conditions->put('enter_id',array('EQ',$Input->post('enter_id')));
			
			$Enter = D('enterprise');
			$Enter->where($conditions)->find();
			$Enter->euser_id = $Input->post('euser_id');
			$Enter->domain = $Input->post('domain');
			$Enter->cid = $Input->post('cid');
			$Enter->name = htmlspecialchars($Input->post('name'));
			if (isset($AllFiles)) {
				$Enter->logo = './upload/'.$AllFiles[0]['savename'];
			}
			$Enter->enote = htmlspecialchars($Input->post('enote'));
			$Enter->tel = htmlspecialchars($Input->post('tel'));
			$Enter->fax = htmlspecialchars($Input->post('fax'));
			$Enter->contactpeople = htmlspecialchars($Input->post('contactpeople'));
			$Enter->address = htmlspecialchars($Input->post('address'));
			$Enter->videocode = $Input->post('videocode');
			$Enter->re_id = (int)$Input->post('re_id');
			$Enter->lock = htmlspecialchars($Input->post('lock'));
			$Enter->videolock = $Input->post('videolock');//视频锁
			$Enter->updated = time();
			$result = $Enter->save();
			if (!$result) {
				halt('尝试修改企业信息失败');
			}
			$this->redirect('AdminIndex','Enterprise');
		}
		//获取要修改的内容
		$conditions = new HashMap();
		$conditions->put('enter_id',array('EQ',$Input->get('enter_id')));
		//获取要修改的企业信息及其关联
		$Enterprise = D('enterprise');
		$Enter = $Enterprise->where($conditions)->xfind();

		//获取企业分类
		$Ecategory = D('ecategory');
		$Ecate = $Ecategory->findall();
		//获取推荐企业类型
		$Recommend = D('recommend');
		$recomm = $Recommend->findall();

		//设置到模板
		$this->assign('Enter',$Enter);
		$this->assign('Ecate',$Ecate); //企业分类
		$this->assign('Recomm',$recomm);//所有推荐类型
		$this->display();
	}
	/**
	 * 删除企业
	 */
	public function AdminDel(){
		EasyACL('Enterprise','AdminDel',$_SESSION);
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		//获取企业编号
		$Enter_id = (int)$Input->get('enter_id');
		$Enterprise = D('enterprise');
		$Re = $Enterprise->deleteById($Enter_id);
		if($Re){
            $this->redirect('AdminIndex','Enterprise');
        } else {
            halt('尝试删除失败!');
        }
	}
	/**
	 * 获取企业信息
	 * 需要POST传入name
	 */
	public function GetEnterpriseInfo(){
		EasyACL('Enterprise','GetEnterpriseInfo',$_SESSION);
		//设置输入
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('name',array('like','%'.$Input->post('name').'%'));
		
		$Enterprise = D('enterprise');
		$E = $Enterprise->where($conditions)->field('enter_id,name')->findall();
		
		$this->assign('E',$E);
		$this->display();
	}
	/**
	 * 获取需要捆绑的域名是否有重复
	 */
	public function AdminGetDomain() {
		EasyACL('Enterprise','AdminGetDomain',$_SESSION);
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		
		//检查值是否存在于系统保留的值内
		$SystemVal = array(
			'Econtactus','Einfoservices','Empty','Enterpriseabout','Eproducts','Euser','Index','Sms_main',
			'econtactus','einfoservices','empty','enterpriseabout','eproducts','euser','index','sms_main',
		);
		if (in_array($Input->post('domain'),$SystemVal)) {
			return 'Y';
		}
		
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('domain',array('EQ',$Input->post('domain')));
		
		$Enterprise = D('Enterprise');
		$Count = $Enterprise->count($conditions,'enter_id');
		if ($Count == 0) {
			echo 'N';
		} else {
			echo 'Y';
		}
	}
}




















